package com.taofuxn.yycreazy.ui.fragment;

import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.taofuxn.yycreazy.R;
import com.taofuxn.yycreazy.adpater.SpecialAdapter;
import com.taofuxn.yycreazy.mvp.BasePresenter;
import com.taofuxn.yycreazy.mvp.MvpFragment;
import com.taofuxn.yycreazy.mvp.model.NewsertJokBean;
import com.taofuxn.yycreazy.mvp.presenter.SpecialPresenter;
import com.taofuxn.yycreazy.mvp.view.ISpecialView;
import com.taofuxn.yycreazy.ports.ITaofuAdapterListener;
import com.taofuxn.yycreazy.ui.view.TaofuSwipeReflashLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;



/**
 * 精选
 * Created by wanghuafu on 16/9/16.
 */
public class SpecialSelectFragment extends MvpFragment implements ISpecialView, SwipeRefreshLayout.OnRefreshListener, ITaofuAdapterListener {

    private SpecialPresenter mSpecialPresenter;
    @InjectView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipeRefreshLayout)
    TaofuSwipeReflashLayout swipeRefreshLayout;


    private SpecialAdapter mSpecialAdapter;
    private ArrayList<NewsertJokBean.NewsertJokResult.NewsertJok> mData;
    private int curpage = 1;
    @Override
    protected int setLayoutId() {
        return R.layout.fragment_special_select;
    }

    @Override
    protected void init() {

        mData = new ArrayList<>();
        swipeRefreshLayout.setColorSchemeColors(Color.BLUE,Color.RED,Color.BLACK,Color.GREEN);
        swipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mSpecialAdapter = new SpecialAdapter(R.layout.item_tab_specialselect,mData);
        mSpecialAdapter.setAdapterListener(this);
        mRecyclerView.setAdapter(mSpecialAdapter);

        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
                onRefresh();
            }
        });

    }

    @Override
    protected BasePresenter createDefaultPresenter() {
        mSpecialPresenter = new SpecialPresenter(this);
        return mSpecialPresenter;
    }

    @Override
    public void reqSuccess(ArrayList<NewsertJokBean.NewsertJokResult.NewsertJok> mData) {
        mSpecialAdapter.setData(mData);
    }

    @Override
    public void dimissLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void loadmoreSuccess(List<NewsertJokBean.NewsertJokResult.NewsertJok> data) {
        mSpecialAdapter.addData(data);
    }

    @Override
    public void onRefresh() {
        curpage = 1;
        mSpecialPresenter.loadData(true,curpage);
    }

    @Override
    public void loadMore() {

        Snackbar.make(mRecyclerView,"正在加载..",Snackbar.LENGTH_SHORT).show();
        curpage += 1;
        mSpecialPresenter.loadData(false,curpage);

    }
}
